package com.yanqu.road.dao.impl.cross.unionwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.UnionWarSessionReward;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionWarSessionRewardDaoImpl extends TempDataObjectDao<UnionWarSessionReward> {


    public Map<Integer, List<UnionWarSessionReward>> getUnionWarSessionReward(long userId, List<Integer> activityIdList) {
        String sql = "select * from t_u_activity_unionwar_session_reward where userid = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ") ";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UnionWarSessionReward>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarSessionReward data = parse(rs);
                if(!resultMap.containsKey(data.getActivityId())){
                    resultMap.put(data.getActivityId(), new ArrayList<>());
                }
                resultMap.get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }


    public static String insertUnionWarSessionRewardSql = "insert into t_u_activity_unionwar_session_reward(activityId,userId,phase,session,unionUid,reward,getReward) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UnionWarSessionReward data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSession()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        if (getDb().executeNoneQuery(insertUnionWarSessionRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionWarSessionRewardSql = "update t_u_activity_unionwar_session_reward set unionUid=?,reward=?,getReward=? where activityId=? and userId=? and phase=? and session=?";
    @Override
    protected boolean updateData(UnionWarSessionReward data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSession()));
        if (getDb().executeNoneQuery(updateUnionWarSessionRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionWarSessionReward parseData(ResultSet rs) throws SQLException {
        UnionWarSessionReward data = new UnionWarSessionReward();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPhase(rs.getInt("phase"));
        data.setSession(rs.getInt("session"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setReward(rs.getString("reward"));
        data.setGetReward(rs.getBoolean("getReward"));
        return data;
    }
}
